using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using IQIGame.Onigao.GameAOT;
using System;

namespace IQIGame.Onigao.GamePlay
{
    public class UIChangeTime : UIBaseWindow
    {
        #region Template Generate,don't modify
        protected class UIB_UIChangeTime
        {
            #region ObjectBinding Generate
            public IQIGame.Onigao.Framework.ExButton btnClose { protected set; get; }
            public IQIGame.Onigao.Framework.ExButton btnConfirm { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textNow { protected set; get; }
            public IQIGame.Onigao.Framework.ExText textTargetTime { protected set; get; }
            public IQIGame.Onigao.Framework.ExText dayOffsetText { protected set; get; }
            public IQIGame.Onigao.Framework.ExInputField inputAddHour { protected set; get; }
            public virtual void InitBinding(ObjectBinding __binding)
            {
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnClose", out var __tbv0);
                this.btnClose = __tbv0;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExButton>("btnConfirm", out var __tbv1);
                this.btnConfirm = __tbv1;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textNow", out var __tbv2);
                this.textNow = __tbv2;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("textTargetTime", out var __tbv3);
                this.textTargetTime = __tbv3;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExText>("dayOffsetText", out var __tbv4);
                this.dayOffsetText = __tbv4;
                __binding.TryGetVariableValue<IQIGame.Onigao.Framework.ExInputField>("inputAddHour", out var __tbv5);
                this.inputAddHour = __tbv5;
            }
            #endregion ObjectBinding Generate 
        }
        #endregion Template Generate,don't modify

        #region Window Config
        /// <summary>
        /// 静态配置
        /// </summary>
        private static UICreateConfig _createConfig = new()
        {
            prefabName = nameof(UIChangeTime),
            layer = EUILayer.Normal,
        };

        /// <summary>
        /// 创建UI的配置
        /// </summary>
        public override IUICreateConfig createConfig => _createConfig;
        #endregion Window Config

        #region fields

        private DateTime _targetTime;

        #endregion fields

        #region properties

        protected UIB_UIChangeTime ui { get; set; }
        protected UIMsgDispatcher uiMsgDispatcher => this._msgDispatcher as UIMsgDispatcher;

        #endregion properties

        protected override void BeforeInit()
        {
            ui = new UIB_UIChangeTime();
            ui.InitBinding(this.csObjBind);
        }

        protected override void OnInit()
        {

        }

        protected override void OnShow(UIBaseData data)
        {
            var curTime = GameDataCenter.ClientDisplayTime;
            var horStr = curTime.Hour.ToString();
            ui.textNow.text = horStr;
            ui.inputAddHour.text = "0";
            ui.textTargetTime.text = horStr;
        }

        protected override void OnHide()
        {

        }

        /// <summary>
        /// 添加UGUI侦听
        /// </summary>
        protected override void AddUGUIListener()
        {
            ui.btnConfirm.AddClickEvent(OnClickBtnConfirm);
            ui.btnClose.AddClickEvent(OnClickBtnClose);
            ui.inputAddHour.onValueChanged.AddListener(OnInputAddHourValueChanged);
        }

        /// <summary>
        /// 添加全局侦听（MsgDispatcher）
        /// </summary>
        protected override void AddMsgListeners()
        {

        }

        protected override void OnDispose()
        {

        }

        #region methonds
        private void OnClickBtnClose()
        {
            Close();
        }

        private void OnClickBtnConfirm()
        {
            Close();
            if (_targetTime == GameDataCenter.ClientDisplayTime)
            {
                return;
            }
            GameDataCenter.SetClientTime(_targetTime);
            GameplayAreaManager.Instance.curSceneArea?.ChangeDisplayTime(_targetTime.Hour);
        }

        private void OnInputAddHourValueChanged(string newValue)
        {
            if (!int.TryParse(newValue, out var addHour))
            {
                LogGame.LogError($"输入的增量非法，检查输入框类型限制。value：{newValue}");
                return;
            }
            if (addHour < 0)
            {
                LogGame.LogError($"小时增量不能小于0。");
                return;
            }
            //计算设置后的实际时间
            var curTime = GameDataCenter.ClientDisplayTime;
            _targetTime = curTime.AddHours(addHour);
            ui.textTargetTime.text = _targetTime.Hour.ToString();
            //计算目标时间和初始时间差了几天
            var timeSpan = _targetTime.Date - curTime.Date;
            var dayOffset = timeSpan.Days;
            var dayOffsetStr = "Error";
            switch (dayOffset)
            {
                case 0:
                    dayOffsetStr = "今日"; //中文文本：后期界面计划要修改，这里不使用ui api，临时处理
                    break;
                case 1:
                    dayOffsetStr = "次日";
                    break;
                default:
                    dayOffsetStr = $"+{dayOffset}日";
                    break;
            }
            ui.dayOffsetText.text = dayOffsetStr;
        }
        #endregion methonds
    }
}